import func from '../utils/func';
import incodeParams from '../utils/incodeParams';
import querystring from '../lib/query-string';
import immutable from '../lib/immutable';
import URL from '../lib/URL';

import { LINK_TYPE_DATA, LINK_TYPE_INPUT, LINK_TYPE_USER } from '../utils/linkType';
import { EVENT_TYPE_PRODUCT_DETAIL } from '../business/product/constrants';
import { EVENT_TYPE_ARTICLE_DETAIL } from '../business/article/constrants';
import { EVENT_TYPE_MULTISHOP_CALL, EVENT_TYPE_CHANGE_SHOP } from '../business/assemble/element/Multishop';

export const EVENT_TYPE_LINK = 'EVENT_TYPE_LINK';// 链接跳转
export const EVENT_TYPE_EMAIL = 'EVENT_TYPE_EMAIL';// 邮箱
export const EVENT_TYPE_CALL = 'EVENT_TYPE_CALL';// 拨打手机
export const EVENT_TYPE_ROUTE_BACK = 'EVENT_TYPE_ROUTE_BACK';// 返回上一级路由
export const EVENT_TYPE_CUSTOM_SERVICE = 'EVENT_TYPE_CUSTOM_SERVICE'; // 联系客服
export const EVENT_TYPE_PAY_CODE = 'EVENT_TYPE_PAY_CODE'; // 付款码
export const EVENT_TYPE_SCAN = 'EVENT_TYPE_SCAN'; // 联系客服

export const EVENT_TYPE_SHARE = 'EVENT_TYPE_SHARE';// 分享

export const HOME = 'HOME';
const Assemble = {
    create: function create(config = {}) {
        const eventHandle = {
            onMail: config.onMail || func,
            onCall: config.onCall || func,
            onService: config.onService || func,
            onPayCode: config.onPayCode || func,
            onJumpBack: config.onJumpBack || func,
            onScan: config.onScan || func,
            onJumpPage: config.onJumpPage || func,
            onChangeShop: config.onChangeShop || func,
            onPayCode: config.onPayCode || func,
            onShare: config.onShare || func,
            urlData: config.urlData || {},
            handle: function handle(event, params = immutable.fromJS({})) {
                if (!event || !event.toJS || !params.toJS) return;
                const query = {};
                const templetId = params.getIn(['templet_id']);
                if (templetId && templetId.length && templetId.length > 0) query.templet_id = templetId;
                const urlMode = event.get('urlMode');
                switch (event.get('id')) {
                    case EVENT_TYPE_PRODUCT_DETAIL: case EVENT_TYPE_ARTICLE_DETAIL: {
                        const pageId = event.get('pageId');
                        const elementId = event.get('elementId');
                        let fieldIdKey,
                            fieldIdValue = '';
                        const listData = params.getIn(['listData']);
                        // 找列表数据里面的对应id值
                        listData && listData.map((value, key) => {
                            const field = listData.getIn([key, 'field']);
                            if (field === 'id') {
                                fieldIdKey = key;
                                fieldIdValue = listData.getIn([key, 'value']);
                            }
                        });
                        const set_id = params.getIn(['set_id']);
                        if (pageId && elementId) { // 判断是否关联了组装的pageId和对应详情组件Id
                            query.page_id = pageId;
                            const x = { set_id };
                            if (fieldIdKey) {
                                x[fieldIdKey] = fieldIdValue;
                            }
                            const cateId = event && event.get('cate_id');
                            if (cateId) {
                                x.cate_id = cateId;
                            }
                            query.parameter = incodeParams({ [elementId]: x });
                            const queryString = querystring.stringify(query);
                            eventHandle.onJumpPage({ pathname: eventHandle.urlData[HOME], query, queryString, urlMode });
                        } else { // 跳转到默认固定业务详情页面
                            query.id = fieldIdValue;
                            query.set_id = set_id;
                            const queryString = querystring.stringify(query);
                            eventHandle.urlData[event.get('id')] && eventHandle.onJumpPage({ pathname: eventHandle.urlData[event.get('id')], query, queryString, urlMode });
                        }
                        break;
                    }
                    case EVENT_TYPE_EMAIL: {
                        const pathname = `mailto:${event.get('value')}`;
                        eventHandle.onMail(event.get('value'), pathname);
                        break;
                    }
                    case EVENT_TYPE_CALL: case EVENT_TYPE_MULTISHOP_CALL: {
                        const pathname = `tel://${event.get('value')}`;
                        eventHandle.onCall(event.get('value'), pathname);
                        break;
                    }
                    case EVENT_TYPE_CUSTOM_SERVICE: {
                        // 触发联系客服事件
                        eventHandle.onService();
                        break;
                    }
                    case EVENT_TYPE_PAY_CODE: {
                        // 触发付款码事件
                        eventHandle.onJumpPage({ pathname: eventHandle.urlData[EVENT_TYPE_PAY_CODE] });
                        break;
                    }
                    case EVENT_TYPE_SCAN: {
                        // 触发扫一扫事件
                        eventHandle.onScan && eventHandle.onScan();
                        break;
                    }
                    case EVENT_TYPE_PAY_CODE: {
                        // 触发付款码事件
                        eventHandle.onPayCode();
                        break;
                    }
                    case EVENT_TYPE_CHANGE_SHOP: {
                        eventHandle.onChangeShop();
                        break;
                    }
                    case EVENT_TYPE_ROUTE_BACK: {
                        eventHandle.onJumpBack();
                        break;
                    }
                    case EVENT_TYPE_SHARE: {
                        eventHandle.onShare();
                        break;
                    }
                    case EVENT_TYPE_LINK: {
                        const value = event.get('value');
                        const pageId = event.get('pageId');
                        const elementId = event.get('elementId');
                        const isUser = event.get('isUser');
                        const set_id = event.get('fieldSetId');
                        const fieldIdKey = event.getIn(['data', 'key']);
                        const fieldIdValue = event.getIn(['data', 'value']);
                        if (event.get('type') === LINK_TYPE_DATA) {
                            if (isUser && pageId && elementId) {
                                query.page_id = pageId;
                                const x = { set_id };
                                if (fieldIdKey) {
                                    x[fieldIdKey] = fieldIdValue;
                                }
                                const cateId = event && event.get('cate_id');
                                if (cateId) {
                                    x.cate_id = cateId;
                                }
                                query.parameter = incodeParams({ [elementId]: x });
                                const queryString = querystring.stringify(query);
                                eventHandle.onJumpPage({ pathname: eventHandle.urlData[HOME], query, queryString, urlMode });
                            }
                        } else if (event.get('type') === LINK_TYPE_USER) {
                            if (pageId) {
                                query.page_id = pageId;
                                const queryString = querystring.stringify(query);
                                eventHandle.onJumpPage({ pathname: eventHandle.urlData[HOME], query, queryString, urlMode });
                            }
                        } else if (event.get('type') === LINK_TYPE_INPUT) {
                            if (!value) { return; }
                            // 与下面的 alse 步骤一直
                            const PATH = URL.parse(value);
                            const query = querystring.parse(PATH.query) || {};
                            const pathname = PATH.pathname;
                            const queryString = querystring.stringify(query);
                            eventHandle.onJumpPage({ pathname, query, queryString, urlMode });
                        } else if (value) { // 系统
                            const PATH = URL.parse(value);
                            const query = querystring.parse(PATH.query) || {};
                            if (query.kind) {
                                query.type = query.kind;
                            }
                            if (event.get('hallId')) {
                                query.id = event.get('hallId');
                            }
                            const pathname = PATH.pathname;
                            const queryString = querystring.stringify(query);
                            eventHandle.onJumpPage({ pathname, query, queryString, urlMode });
                        }
                        break;
                    }
                    default: {
                        break;
                    }
                }
            },
        };
        return eventHandle;
    },
};
export default Assemble;
